package priv.ln.team.view;

import priv.ln.team.domain.Employee;
import priv.ln.team.domain.Programmer;
import priv.ln.team.service.NameListService;
import priv.ln.team.service.TeamException;
import priv.ln.team.service.TeamService;

/**
 * ClassName: TeamView
 * Package: priv.ln.team.view
 * Description:
 *
 * @Author 辣辣
 * @Create 2025/4/17 17:35
 * @Version 1.0
 */
public class TeamView {
    private NameListService listService = new NameListService();
    private TeamService teamService = new TeamService();

    public void enterMainMenu() {
        boolean flag = true;
        do {
            listAllEmployees();
            System.out.print("1-团队列表  2-添加团队成员  3-删除团队成员 4-退出   请选择(1-4)：");
            char selection = TSUtility.readMenuSelection();
            switch (selection) {
                case '1':
                    getTeam();
                    break;
                case '2':
                    addMember();
                    break;
                case '3':
                    deleteMember();
                    break;
                case '4':
                    char c = TSUtility.readConfirmSelection();
                    System.out.print("确认是否删除(Y/N):");
                    if(c == 'Y'){
                        flag = false;
                    }
                    break;
            }
        } while (flag);
    }

    private void deleteMember() {
        System.out.println("---------------------删除成员---------------------");
        System.out.print("请输入要删除员工的TID：");
        int tid = TSUtility.readInt();
        System.out.print("确认是否删除(Y/N):");
        char c = TSUtility.readConfirmSelection();
        if(c == 'Y'){
            try {
                teamService.removeMember(tid);
            } catch (TeamException e) {
                System.out.println(e.getMessage());
            }
            TSUtility.readReturn();
        }
    }

    private void addMember() {
        System.out.println("---------------------添加成员---------------------");
        System.out.print("请输入要添加的员工ID：");
        int id = TSUtility.readInt();
        try {
            teamService.addMember(listService.getEmployee(id));
        } catch (TeamException e) {
            System.out.println(e.getMessage());
        }
        TSUtility.readReturn();
    }

    private void getTeam() {
        Programmer[] team = teamService.getTeam();
        System.out.println("--------------------团队成员列表---------------------");
        System.out.println("TDI/ID\t姓名\t年龄\t工资\t职位\t奖金\t股票");
        for (int i = 0; i < teamService.getTotal(); i++) {
            System.out.println(team[i].getMemberId() + "/" + team[i].getId() + "\t" + team[i].toString());
        }
        System.out.println("-----------------------------------------------------");
    }

    private void listAllEmployees(){
        System.out.println("-----------------------------------优尚开发团队调度系统------------------------------------");
        System.out.println("ID\t姓名\t年龄\t工资\t职位\t状态\t奖金\t股票\t领用设备");
        Employee[] allEmployees = listService.getAllEmployees();
        for (int i = 0; i < allEmployees.length; i++) {
            System.out.println(allEmployees[i].getId() + "\t" + allEmployees[i].toString());
        }
        System.out.println("----------------------------------------------------------------------------------------------");
    }

    public static void main(String[] args) {
        new TeamView().enterMainMenu();
    }
}
